﻿using System;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Commands;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Roles;
using Module.Core.LoginSystem.Components.Users;

namespace Web.Cms.Users
{
    public partial class UserPassword : AdminUserControl
    {
        private int _userId;
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            CommandInfo _commandInfo = CommandController.GetCommand("admin_usermanager");
            RolePermisionInfo _rolePermesionInfo =
                   RoleController.GetReadWritePermison(CurrentUser.UserID, _commandInfo.CommandID);
            if (_rolePermesionInfo == null) return;
            if (_rolePermesionInfo.Write == 0)
            {
                btnUpdate.Visible = false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _userId = ConvertUtility.ToInt32(Request.QueryString["userpasswordid"]);
            if (!Page.IsPostBack)
            {
                UserInfo _user = UserController.GetUser(_userId);
                if (_user == null) return;
                lblUser.Text = _user.Username;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text != string.Empty)
            {
                UserController.ResetPasswordByUserID(_userId, SecurityUtility.MD5Encrypt(txtPassword.Text));
                // Insert Cores_Audits
                UserInfo _userInfo = UserController.GetUser(_userId);
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                /*
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID, _userInfo.UserID
                                            , "Đã thay đổi password của :" + _userInfo.FirstName + " " + _userInfo.LastName, 1, DateTime.Now);
                 */
                Response.Redirect(
                        UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { },
                                            new string[] { }));
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { }, new string[] { }));
        }
    }
}